package com.ly.service.impl;

import com.ly.mapper.PaymentSelectionMapper;
import com.ly.po.PaymentPO;
import com.ly.pojo.PaymentSelection;
import com.ly.service.PaymentSelectionService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class PaymentSelectionServiceImpl implements PaymentSelectionService {

    @Autowired
    private PaymentSelectionMapper paymentSelectionMapper;

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public void generateSelection(PaymentPO paymentPO) {
        // 加锁
        RLock lock = redissonClient.getLock("pay:" + paymentPO.getPayerId() + ":" + paymentPO.getId());

        try {
            lock.tryLock(5,30, TimeUnit.SECONDS);

            //  插入之前先查询，若有则删除
            PaymentSelection paymentSelection1 = paymentSelectionMapper.selectByPayIdAndPayerId(paymentPO.getId(), paymentPO.getPayerId());
            if (paymentSelection1 != null) {
                //说明有，删除
                paymentSelectionMapper.deleteOne(paymentPO.getId(), paymentPO.getPayerId());
            }
            PaymentSelection paymentSelection = new PaymentSelection();
            paymentSelection.setPayerId(paymentPO.getPayerId());
            paymentSelection.setPayId(paymentPO.getId());
            paymentSelection.setPayStatus(0);
            paymentSelection.setPayTime(new Date());
            paymentSelection.setPayMethod("支付宝");

            paymentSelectionMapper.insert(paymentSelection);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }

    @Override
    public Boolean updateStatus(int payId, Long payerId) {
        PaymentSelection paymentSelection = new PaymentSelection();
        paymentSelection.setPayId(payId);
        paymentSelection.setPayerId(payerId);
        paymentSelection.setPayMethod("支付宝");
        paymentSelection.setPayTime(new Date());
        paymentSelection.setPayStatus(1);
        int i = paymentSelectionMapper.updateByPayIdAndPayerId(paymentSelection);
        if (i == 1) {
            return true;
        }
        return false;
    }

    @Override
    public void deleteOne(Integer id, Long payerId) {
        paymentSelectionMapper.deleteOne(id, payerId);
    }
}
